package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.dao.NoticeMapper;
import com.bbt.dormassistant.dao.UserInformationMapper;
import com.bbt.dormassistant.entity.Notice;
import com.bbt.dormassistant.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Resource
    NoticeMapper noticeMapper;


    @Resource
    UserInformationMapper userInformationMapper;


    /**
     *
     * @param userId 用户ID
     * @param tittle 标题
     * @param text 内容
     * @return Boolean
     */
    @Override
    public Boolean addNotice(String userId, String tittle,String text) {
        try {

            if(userInformationMapper.selectByPrimaryKey(userId) != null && userInformationMapper.selectByPrimaryKey(userId).getIsManger() != 1 ) return false;
            Notice notice = new Notice();
            notice.setUserId(userId);
            notice.setTittle(tittle);
            notice.setText(text);

            notice.setEnd(new Date());
            return noticeMapper.insert(notice) == 1;
        }catch (Exception e ){
            e.printStackTrace();
            return false;
        }
    }


    /**
     *
     * @param noticeId 需要被删除的告示ID
     * @return Boolean
     */
    @Override
    public Boolean deleteNotice(int noticeId) {
        try {
           return noticeMapper.deleteByPrimaryKey(noticeId) == 1;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    /**
     *
     * @param currIndex 当前页
     * @param pageSize 页大小
     * @return List<Notice>
     */
    @Override
    public List<Notice> queryAllNotice(Integer currIndex,Integer pageSize) {
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",currIndex==null? 0:(currIndex-1)*pageSize);
        data.put("pageSize",pageSize==null? 10000:pageSize);
        return noticeMapper.selectAll(data);
    }

}
